Root Zanli
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
o5t6x7pgljbm
/
public_html
/
admin
/
app
/
V2
/
BulkOperations
/
Importers
/
Filename :
GroupsImporter.php
back
Copy
<?php namespace App\V2\BulkOperations\Importers; use App\Libraries\FileStorageSystem; use App\Models\CoinsForCollegeRole; use App\Models\Role; use App\V2\BulkOperations\ImportFactories\FieldValidatorFactory; use App\V2\BulkOperations\ImportFields\DataImportField; use App\V2\BulkOperations\ImportFieldTransformers\DownloadImageFieldTransformer; use App\V2\BulkOperations\ImportFieldTransformers\PasswordHashFieldTransformer; use App\V2\BulkOperations\ImportFieldTransformers\RoleIdFieldTransformer; use App\V2\BulkOperations\ImportServices\ImportLoggingService; use App\V2\BulkOperations\ImportServices\TempTableService; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; class GroupsImporter extends BaseImporter { private $fields = []; private $additional_fields = []; public function __construct($importProfile) { parent::__construct($importProfile); } private function populateFields(){ $this->fields['group_name'] = new DataImportField('group_name','group_name', 'VARCHAR(255)', [ // FieldValidatorFactory::getFieldValidator(FieldValidatorFactory::STRING_VALIDATOR_0_255_ALPHANUM), FieldValidatorFactory::getFieldValidator(FieldValidatorFactory::NOT_NULL_VALIDATOR), ]); $this->fields['group_image_url'] = new DataImportField('group_image_url','group_image_url', 'TEXT', [ FieldValidatorFactory::getFieldValidator(FieldValidatorFactory::STRING_VALIDATOR_URL), ]); $this->fields['owner_email'] = new DataImportField('owner_email','owner_email', 'VARCHAR(255)', [ FieldValidatorFactory::getFieldValidator(FieldValidatorFactory::STRING_VALIDATOR_0_255_EMAIL), FieldValidatorFactory::getFieldValidator(FieldValidatorFactory::NOT_NULL_VALIDATOR), ]); //other fields to be populated with default value or with transformed values: $this->fields['local_group_image_url'] = new DataImportField('local_group_image_url', 'local_group_image_url', 'VARCHAR(255)', []); $this->fields['group_uuid'] = new DataImportField('group_uuid', 'group_uuid', 'VARCHAR(255)', []); $this->fields['team_uuid'] = new DataImportField('team_uuid', 'team_uuid', 'VARCHAR(255)', []); $this->fields['group_name']->setIsRequired(true); $this->fields['owner_email']->setIsRequired(true); $this->fields['group_image_url']->setIsRequired(true); $this->fields['group_uuid']->setAutoGenerateType(DataImportField::AUTO_GENERATE_TYPE_STR_UUID); $this->fields['team_uuid']->setAutoGenerateType(DataImportField::AUTO_GENERATE_TYPE_STR_UUID); $this->fields['local_group_image_url']->setTransformer( new DownloadImageFieldTransformer($this->fields['local_group_image_url'], [$this->fields['group_image_url']], 'group_images', 'default-group-image-color.jpg') ); } private function populateProfileSpecificAdditionalFields(){ $this->additional_fields['owner_user_id'] = new DataImportField('owner_user_id', 'owner_user_id', 'INT', []); $this->additional_fields['group_id'] = new DataImportField('group_id', 'group_id', 'INT', []); $this->additional_fields['team_id'] = new DataImportField('team_id', 'team_id', 'INT', []); } protected function getFields(): array { if(count($this->fields) == 0) $this->populateFields(); return $this->fields; } protected function getProfileSpecificAdditionalFields(): array { if(count($this->additional_fields) == 0) $this->populateProfileSpecificAdditionalFields(); return $this->additional_fields; } protected function getMainTableId(){ return 'group_id'; } protected function validateProfileSpecificData() { //Validation logic // 1, validation owner user exists $this->validateOwnerUserExists(); // 2, populate owner user id $this->populateUserIdToTempTable(); } public function importToMainProfileSpecificData() { //Insert into main tables // 1, insert into groups table $this->insertIntoGroupsTable(); // 2, populate group_id in temp table $this->populateGroupId(); // 3, insert into teams table $this->insertIntoTeamsTable(); // 4, populate team_id column in temp table $this->populateTeamId(); // 5, populate team_id column in `groups` table $this->updateTeamIdInGroups(); // 5, insert into group_members table $this->insertIntoGroupMembers(); //6, insert into user_roles table $this->insertIntoRoleUser(); } protected function validateOwnerUserExists(){ $field = $this->getAllFields()['owner_email']; $record_is_valid_column_name = $this->getAllFields()['validate_is_valid']->getColumnName(); $record_errors_column_name = $this->getAllFields()['validate_errors']->getColumnName(); $total_records = $this->importQueueItem->additionalDetails->total_records; $error_message = "user does not present with provided {$field->getColumnName()}"; $validationQuery = "UPDATE {$this->importQueueItem->temp_table_name} AS tmp "; $validationQuery .= " LEFT JOIN users AS u "; $validationQuery .= " ON u.email = tmp.{$field->getColumnName()} " ; $validationQuery .= " SET tmp.{$record_is_valid_column_name} = false "; $validationQuery .= ", tmp.{$record_errors_column_name} = " . " IF(tmp.{$record_errors_column_name} IS NULL, '{$error_message}' ," . " CONCAT( tmp.{$record_errors_column_name}, ', ', '{$error_message}') ) "; $validationQuery .= " WHERE u.user_id IS NULL OR u.deleted_at IS NOT NULL"; Log::debug("Running Update Query: ". $validationQuery); $this->tempTableService->executeUpdateInBatches($validationQuery, 'tmp.'.$this->pk_column_name, $this->batch_size, $total_records); return ['error' => false, 'error_message' => '']; } protected function populateUserIdToTempTable(){ $field = $this->getAllFields()['owner_email']; $ref_id_field = $this->getAllFields()['owner_user_id']; $total_records = $this->importQueueItem->additionalDetails->total_records; $validationQuery = "UPDATE {$this->importQueueItem->temp_table_name} AS tmp "; $validationQuery .= " LEFT JOIN `users` AS u "; $validationQuery .= " ON u.email = tmp.{$field->getColumnName()} " ; $validationQuery .= " SET tmp.{$ref_id_field->getColumnName()} = u.user_id "; $validationQuery .= " WHERE u.user_id IS NOT NULL AND u.deleted_at IS NULL "; Log::debug("Running Update Query: ". $validationQuery); $this->tempTableService->executeUpdateInBatches($validationQuery, 'tmp.'.$this->pk_column_name, $this->batch_size, $total_records); return ['error' => false, 'error_message' => '']; } protected function populateTeamId(){ $field = $this->getAllFields()['team_uuid']; $ref_id_field = $this->getAllFields()['team_id']; $total_records = $this->importQueueItem->additionalDetails->total_records; $validationQuery = "UPDATE {$this->importQueueItem->temp_table_name} AS tmp "; $validationQuery .= " LEFT JOIN `teams` AS t "; $validationQuery .= " ON t.uuid = tmp.{$field->getColumnName()} " ; $validationQuery .= " SET tmp.{$ref_id_field->getColumnName()} = t.id "; $validationQuery .= " WHERE t.id IS NOT NULL "; Log::debug("Running Update Query: ". $validationQuery); $this->tempTableService->executeUpdateInBatches($validationQuery, 'tmp.'.$this->pk_column_name, $this->batch_size, $total_records); return ['error' => false, 'error_message' => '']; } protected function updateTeamIdInGroups(){ $field = $this->getAllFields()['group_id']; $ref_id_field = $this->getAllFields()['team_id']; $validate_is_valid_field_name = $this->getAllFields()['validate_is_valid']->getColumnName(); $total_records = $this->importQueueItem->additionalDetails->total_records; $validationQuery = "UPDATE {$this->importQueueItem->temp_table_name} AS tmp "; $validationQuery .= " JOIN `groups` AS g "; $validationQuery .= " ON g.group_id = tmp.{$field->getColumnName()} " ; $validationQuery .= " SET g.team_id = tmp.{$ref_id_field->getColumnName()}"; $validationQuery .= " WHERE tmp.{$validate_is_valid_field_name} = 1 "; Log::debug("Running Update Query: ". $validationQuery); $this->tempTableService->executeUpdateInBatches($validationQuery, 'tmp.'.$this->pk_column_name, $this->batch_size, $total_records); return ['error' => false, 'error_message' => '']; } private function insertIntoGroupsTable(){ //TODO: update password, api_token and profile_pic, user_uuid $validate_is_valid_field_name = $this->getAllFields()['validate_is_valid']->getColumnName(); $total_records = $this->importQueueItem->additionalDetails->total_records; $insert_sql = "INSERT INTO `groups`(uuid, owner_user_id, group_name, group_image, created_at, import_queue_item_id) "; $select_sql = " SELECT group_uuid, owner_user_id, group_name, local_group_image_url, NOW(), {$this->importQueueItem->import_queue_item_id} AS import_queue_item_id " . " FROM {$this->importQueueItem->temp_table_name} AS tmp "; $select_sql .= " WHERE tmp.{$validate_is_valid_field_name} = 1 "; $this->tempTableService->executeUpdateInBatches($insert_sql . $select_sql, $this->pk_column_name, $this->batch_size, $total_records ); } private function insertIntoTeamsTable(){ $validate_is_valid_field_name = $this->getAllFields()['validate_is_valid']->getColumnName(); $total_records = $this->importQueueItem->additionalDetails->total_records; $insert_sql = "INSERT INTO `teams`(uuid, name, display_name, description, " . " created_at, import_queue_item_id) "; $select_sql = " SELECT team_uuid, group_name, group_name, group_name, " . " NOW(), {$this->importQueueItem->import_queue_item_id} AS import_queue_item_id " . " FROM {$this->importQueueItem->temp_table_name} AS tmp "; $select_sql .= " WHERE tmp.{$validate_is_valid_field_name} = 1 "; $this->tempTableService->executeUpdateInBatches($insert_sql . $select_sql, $this->pk_column_name, $this->batch_size, $total_records ); } private function insertIntoGroupMembers(){ $coins_for_college_role_id_parent = CoinsForCollegeRole ::whereIn('role_name', ['Legal guardian of a child','Adult']) ->first()->coins_for_college_role_id; $validate_is_valid_field_name = $this->getAllFields()['validate_is_valid']->getColumnName(); $total_records = $this->importQueueItem->additionalDetails->total_records; $insert_sql = "INSERT INTO `group_members`(group_id, member_user_id, email, role_id, invitation_accepted, is_active, created_at, import_queue_item_id) "; $select_sql = " SELECT group_id, owner_user_id, owner_email, $coins_for_college_role_id_parent AS coins_for_college_role_id," . " 1 AS invitation_accepted, 1 AS is_active, NOW(), " . " {$this->importQueueItem->import_queue_item_id} AS import_queue_item_id " . " FROM {$this->importQueueItem->temp_table_name} AS tmp"; $select_sql .= " WHERE tmp.{$validate_is_valid_field_name} = 1 "; $this->tempTableService->executeUpdateInBatches($insert_sql . $select_sql, $this->pk_column_name, $this->batch_size, $total_records ); } // TODO: yet to implement private function insertIntoRoleUser(){ $parent_role_id = Role::where('name', 'PARENT')->first()->id; $validate_is_valid_field_name = $this->getAllFields()['validate_is_valid']->getColumnName(); $total_records = $this->importQueueItem->additionalDetails->total_records; $insert_sql = "INSERT INTO `role_user`( role_id, user_id, user_type, team_id, import_queue_item_id) "; $select_sql = " SELECT $parent_role_id, owner_user_id, 'App\\Models\\User' AS user_type, team_id, {$this->importQueueItem->import_queue_item_id} AS import_queue_item_id " . " FROM {$this->importQueueItem->temp_table_name} AS tmp"; $select_sql .= " WHERE tmp.{$validate_is_valid_field_name} = 1 "; $this->tempTableService->executeUpdateInBatches($insert_sql . $select_sql, $this->pk_column_name, $this->batch_size, $total_records ); } protected function populateGroupIdToTempTable(){ $field = $this->getAllFields()['sp_wallet_uid']; $ref_id_field = $this->getAllFields()['sp_wallet_id']; $validate_is_valid_field_name = $this->getAllFields()['validate_is_valid']->getColumnName(); $total_records = $this->importQueueItem->additionalDetails->total_records; $validationQuery = "UPDATE {$this->importQueueItem->temp_table_name} AS tmp "; $validationQuery .= " LEFT JOIN `sp_wallet` AS wallet "; $validationQuery .= " ON wallet.wallet_uid = tmp.{$field->getColumnName()} " ; $validationQuery .= " SET tmp.{$ref_id_field->getColumnName()} = wallet.sp_wallet_id "; $validationQuery .= " WHERE wallet.sp_wallet_id IS NOT NULL AND wallet.deleted_at IS NULL "; $validationQuery .= " AND tmp.{$validate_is_valid_field_name} = 1 "; Log::debug("Running Update Query: ". $validationQuery); $this->tempTableService->executeUpdateInBatches($validationQuery, 'tmp.'.$this->pk_column_name, $this->batch_size, $total_records); return ['error' => false, 'error_message' => '']; } } ?>